package com.adams.app.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.BaseColumns;
import android.support.v4.content.CursorLoader;
import android.util.Log;
import com.adams.app.model.Food;
import com.adams.app.utils.database.Column;
import com.adams.app.utils.database.SQLiteTable;

public class FoodsDateHelper extends BaseDataHelper {

	private String foodName;
	
	public FoodsDateHelper(Context context, String foodName) {
		super(context);
		this.foodName = foodName;
		Log.e("菜单", foodName);
	}

	@Override
	protected Uri getContentUri() {
		return DataProvider.FOODS_CONTENT_URI;
	}

	private ContentValues getContentValues(Food food) {
		ContentValues values = new ContentValues();
		values.put(FoodsDBInfo.ID, food.id);
		values.put(FoodsDBInfo.TYPE, foodName);
		values.put(FoodsDBInfo.JSON, food.toJson());
		return values;
	}

	// 查询
	public Food query(long id) {
		Food food = null;
		String selection = FoodsDBInfo.TYPE + " = ? " + " AND " + FoodsDBInfo.ID + "= ?";
		Cursor cursor = query(null, selection,
				new String[] {foodName, String.valueOf(id)}, null);
		if (cursor.moveToFirst()) {
			food = Food.fromCursor(cursor);
		}
		cursor.close();
		return food;
	}

	// 快捷插入
	public void bulkInsert(List<Food> foods) {
		ArrayList<ContentValues> contentValues = new ArrayList<ContentValues>();
		for (Food food : foods) {
			ContentValues values = getContentValues(food);
			contentValues.add(values);
		}
		ContentValues[] valueArray = new ContentValues[contentValues.size()];
		bulkInsert(contentValues.toArray(valueArray));
	}

	// 删除所有
	public int deleteAll() {
		synchronized (DataProvider.DBLock) {
			DataProvider.DBHelper mDBHelper = DataProvider.getDBHelper();
			SQLiteDatabase db = mDBHelper.getWritableDatabase();
			int row = db.delete(FoodsDBInfo.TABLE_NAME, FoodsDBInfo.TYPE + " =? ", 
					new String[] {foodName});
			return row;
		}
	}

	// 获取加载器
	public CursorLoader getCursorLoader() {
		if (null == getContext()) {
			Log.i("CursorLoader", "null?");
		} else {
			Log.i("CursorLoader", "not null");
		}
		return new CursorLoader(getContext(), getContentUri(), null,
				 FoodsDBInfo.TYPE + "=?",
				 new String[] {foodName}, 
				 FoodsDBInfo._ID + " ASC");
	}

	
	public static final class FoodsDBInfo implements BaseColumns {
		private FoodsDBInfo() {
		}

		public static final String TABLE_NAME = "foods";

		public static final String TYPE = "type";
		 
		public static final String ID = "id";

		public static final String JSON = "json";

		public static final SQLiteTable TABLE = new SQLiteTable(TABLE_NAME)
				.addColumn(ID, Column.DataType.INTEGER)
				.addColumn(TYPE,  Column.DataType.TEXT)
				.addColumn(JSON, Column.DataType.TEXT);
	}

}
